// Created by wangliang on 2025/2/12.

import 'package:flutter/material.dart';
import 'package:flutter_app_demo/base/widget/common_app_bar.dart';
import 'package:flutter_app_demo/pages/web/common_web_page_view_model.dart';
import 'package:flutter_app_demo/utils/logger.dart';
import 'package:flutter_inappwebview/flutter_inappwebview.dart';
import 'package:go_router/go_router.dart';
import 'package:mvvm_plus/mvvm_plus.dart';

class CommonWebPage extends ViewWidget<CommonWebPageViewModel> {
  final String url;

  CommonWebPage({super.key, required this.url}) : super(builder: () => CommonWebPageViewModel(url: url));

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: CommonAppBar(
        onBackPressed: () => context.pop(),
        title: '网页',
      ),
      body: _buildBody(),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          viewModel.callAsyncJs();
        },
        tooltip: 'Increment',
        child: const Icon(Icons.add),
      ),
    );
  }

  Widget _buildBody() {
    return InAppWebView(
      initialFile: "test.html",
      onWebViewCreated: (controller) {
        viewModel.initController(controller);
      },
      onLoadStart: (controller, url) {
        Log.d("WLTest onLoadStart $url");
      },
      onLoadStop: (controller, url) {
        Log.d("WLTest onLoadStop $url");
      },
      onReceivedHttpError: (controller, request, errorResponse) {
        Log.d("WLTest onReceivedHttpError ${errorResponse.statusCode}");
      },
    );
  }
}
